﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Linq;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;

//Methods which are callable from scripts. These will also raise an event related to them.
namespace NewsWorm {
    public static class ScriptingMethods {

        internal static IFeedManager m_FeedManager;
        internal static TreeView m_View;
        internal static MainMenu m_Menu;
        internal static ScriptingEvents m_Events;
        internal static IMainWindowClient m_Client;

        public delegate void MenuItemActionDelegate();

        public static bool MarkArticleAsRead( IArticle item ) {
            try {
                m_Client.MarkArticleAsRead( item.ArticleUrl );
                return true;
            } catch {
                return false;
            }
        }

        public static bool AddArticleToFavorites( IArticle item ) {
            try {
                m_Client.MarkAsFavorite(item.ArticleUrl);
                return true;
            } catch {
                return false;
            }
        }

        public static bool DeleteFeed( IFeed feed ) {
            try {
                m_Client.RemoveOneFeed( feed.Title );
                return true;
            } catch {
                return false;
            }
        }

        public static List<IFeed> GetAllFeeds() {
            List<IFeed> feeds = new List<IFeed>();

            foreach ( Feed feed in m_FeedManager.Feeds ) {
                feeds.Add( feed );
            }

            return feeds;
        }

        public static bool DeleteFavorite( IFavoriteArticle favorite ) {
            try {
                TreeNode ToBeRemovedNode = null;
                FavoriteItem ToBeRemovedFavorite = null;

                foreach ( TreeNode node in m_View.Nodes ) {
                    if ( node.Level == 0 && node.Name == "fav" ) {
                        foreach ( TreeNode sub in node.Nodes ) {
                            if ( sub.Name.Equals( favorite.Url ) ) {
                                ToBeRemovedNode = sub;
                                break;
                            }
                        }
                    }
                }

                foreach ( FavoriteItem item in m_FeedManager.StarredItems ) {
                    if ( item.Url.Equals( favorite.Url ) ) {
                        ToBeRemovedFavorite = item;
                        break;
                    }
                }

                m_View.Nodes.Remove( ToBeRemovedNode );
                m_FeedManager.StarredItems.Remove( ToBeRemovedFavorite );
                m_Events.RaiseOnRemoveFavorite( favorite );

                return true;
            } catch {
                return false;
            }

        }

        public static void OpenArticle( IArticle item ) {
            try {
                Process.Start( item.ArticleUrl );
            } catch ( Exception e ) {
                Console.WriteLine( e.Message );
            }
        }

        public static void AddMenuItem( string title, MenuItemActionDelegate func ) {
            MenuItem item = new MenuItem( title);
            MenuItem RootItem = new MenuItem( "Scripts" );
            RootItem.Name = "Scripts";

            bool HasScriptsMenu = false;
            item.Click += new EventHandler( (object sender, EventArgs e) => { func(); });

            foreach ( MenuItem i in m_Menu.MenuItems ) {
                if ( i.Text.Equals( RootItem.Text ) )
                    HasScriptsMenu = true;
            }
            
            if ( !HasScriptsMenu ) {
                RootItem.MenuItems.Add( item );
                m_Menu.MenuItems.Add( 3, RootItem );
            } else {
                MenuItem[] items = m_Menu.MenuItems.Find( RootItem.Name, false );
                items[0].MenuItems.Add( item );
            }
        }


    }
}
